// sho.effects.Explode v. 0.6
//
// This code is released under the creative commons license. If you use this code, 
// give me attribution if you feel like it. :-)
//
// This code has not been extensively tested, and may have lots and lots of bugs;
// use at your own risk.
//
// v. 0.6 -- ported to beta 3.
// v. 0.5 -- first public release

package sho.effects 
{
	import flash.display.DisplayObject;
	import flash.events.Event;

	import mx.core.UIComponent;
	import mx.effects.*;
	import mx.managers.PopUpManager;

	import sho.util.BitmapUtil;
	import mx.controls.Alert;
	import mx.events.EffectEvent;

	public class Explode extends Parallel 
	{
		[Inspectable]
		public var zoomFactor : Number = 2;
		
		protected var proxies : Array = [];
		
		public function Explode(target: Object = null)
		{
			super(target);
			addEventListener(EffectEvent.EFFECT_END, _onEffectEnd);
		}
		
		public override function play(theTargets:Array=null, reversed:Boolean=false):Array
		{
			// We don't want to have this effect play while
			// it is playing, because it makes it harder
			// to manage the list of currently animating
			// proxies. (not rocket science, but probably 
			// not a super important use case)
			if (this.isPlaying)
				return [];

			// The targets array can come in three forms: 
			// it may have been passed to us, it may come
			// from this.targets, or there may only be 
			// one target in this.target. Normalize all
			// three of these cases into a targets array.
			//
			if (theTargets == null)
				theTargets = targets;
			
			if (theTargets == null)
			{
				if (target != null)
					theTargets = [target];
			}

			// If we have a targets array...
			if (theTargets != null)
			{
				// Delete all current children of this effect.
				this.children = [];

				// Add a zoom and a fade for each child.
				for each (var thisTarget : UIComponent in theTargets)
				{
					var proxy : UIComponent;
					
					// Create a bitmap proxy for this target.
					proxy = BitmapUtil.renderAsPopup(thisTarget as UIComponent);

					// Add the proxy to the proxies array.					
					proxies.push(proxy);

				    // Create a zoom, fade and move effect for this bitmap
				    // and add them as children of this effect.
					var zoom : Zoom = new Zoom();
					zoom.zoomWidthTo = zoomFactor;
					zoom.zoomHeightTo = zoomFactor;
					zoom.duration = duration;
					zoom.target = proxy;
					addChild(zoom);
					
					var fade : Fade = new Fade();
					fade.alphaTo = 0;
					fade.duration = duration;
					fade.target = proxy;
					addChild(fade);
					
					var move : Move = new Move();
					move.xBy = - (zoomFactor-1) * thisTarget.width / 2;
					move.yBy = - (zoomFactor-1) * thisTarget.height / 2;
					move.duration = duration;
					move.target = proxy;
					addChild(move);
				}
				
				super.play(theTargets, reversed);
			}
			
			// No properties are modified by this effect.
			return [];
		}
		
		private function _onEffectEnd(event: Event) : void
		{
			for each (var proxy: UIComponent in proxies)
			{
				PopUpManager.removePopUp(proxy);
			}
			
			proxies = [];
		}
	}
}